package vista;

import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.JButton;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;

import modelo.dao.DAO;
import controlador.Controlador;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class PanelHMPedidos extends JPanel {
	private JTable table;
	private JTable table_1;
	private JButton btandir;
	private JButton btnEliminar;
	private JLabel lblPlatos;
	private JTextField txtPedido;
	private JButton btnBebidas;
	private JButton btnPrimeros;
	private JButton btnSegundos;
	private JButton btnPostres;
	private JButton btnEntrantes;
	private JButton btnPescados;
	private JButton btnCarnes;
	private JButton btnPastas;
	private JButton btnVolver;
	private DAO dao;
	private Controlador controlador;
	private JPanel actual;
	private DefaultTableModel model;
	private DefaultTableModel model_1;
	private String[] productos;
	private final String[] bebidas= {"CocaCola","Fanta naranja","Lambrusco"};
	private final String[] postres= {"Tarta chocolate","Flan","Tiramisu"};

	/**
	 * Create the panel.
	 */
	public PanelHMPedidos(DAO d, Controlador c) {
		
	//	this.bebidas = {"CocaCola","Fanta naranja","Lambrusco"};
		
		setBackground(Color.GRAY);
		setLayout(null);
		setSize(1024,768);
		this.dao = d;
		this.controlador = c;
		this.actual = this;
		
		table = new JTable();
		table.setBounds(456, 91, 339, 422);
		model = new DefaultTableModel(
				new Object[][] {
						
				},
				new String[] {
					"Producto"
				}
				) {
					boolean[] columnEditables = new boolean[] {
						false, false
					};
					public boolean isCellEditable(int row, int column) {
						return columnEditables[column];
					}
		};
		
		table.setModel(model);
		
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.getColumnModel().getColumn(0).setResizable(false);
		
		
		add(table);
		
		table_1 = new JTable();
		
		model_1 = new DefaultTableModel(
				new Object[][] {
						
				},
				new String[] {
					"Producto","Cantidad"
				}
				) {
					boolean[] columnEditables = new boolean[] {
						false, true
					};
					public boolean isCellEditable(int row, int column) {
						return columnEditables[column];
					}
		};
		
		table_1.setModel(model_1);
		table_1.setBounds(10, 91, 339, 422);
		
		table_1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		
		//table_1.getColumnModel().getColumn(0).setWidth(100);
		table_1.getColumnModel().getColumn(0).setResizable(false);
		
		//table_1.getColumnModel().getColumn(0).
		
		add(table_1);
		
		btandir = new JButton("A\u00F1adir");
		btandir.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				int i = table.getSelectedRow();
				//table.getse
				model_1.addRow(new Object[]{productos[i]});
			}
		});
		btandir.setBounds(359, 91, 89, 73);
		add(btandir);
		
		btnEliminar = new JButton("Eliminar");
		btnEliminar.setBounds(357, 175, 89, 73);
		add(btnEliminar);
		
		lblPlatos = new JLabel("Listado de Platos");
		lblPlatos.setHorizontalAlignment(SwingConstants.CENTER);
		lblPlatos.setBounds(456, 50, 339, 14);
		add(lblPlatos);
		
		txtPedido = new JTextField();
		txtPedido.setText("Pedido N\u00FAmero X");
		txtPedido.setBounds(10, 47, 141, 20);
		add(txtPedido);
		txtPedido.setColumns(10);
		
		btnBebidas = new JButton("Bebidas");
		btnBebidas.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				productos = bebidas;
				model.setRowCount(0);
				for (int i = 0; i < bebidas.length; i++){
					model.addRow(new Object[]{bebidas[i]});
				}
				
			}
		});
		btnBebidas.setBounds(10, 524, 189, 23);
		add(btnBebidas);
		
		btnPrimeros = new JButton("Carnes/Pescados");
		btnPrimeros.setBounds(208, 524, 189, 23);
		add(btnPrimeros);
		
		btnSegundos = new JButton("Embutidos");
		btnSegundos.setBounds(404, 524, 189, 23);
		add(btnSegundos);
		
		btnPostres = new JButton("L\u00E1cteos");
		btnPostres.setBounds(603, 524, 189, 23);
		add(btnPostres);
		
		btnEntrantes = new JButton("Postres");
		btnEntrantes.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				productos = postres;
				model.setRowCount(0);
				for (int i = 0; i < postres.length; i++){
					model.addRow(new Object[]{postres[i]});
				}
				
			}
		});
		btnEntrantes.setBounds(10, 568, 189, 23);
		add(btnEntrantes);
		
		btnPescados = new JButton("Frutas/Verduras");
		btnPescados.setBounds(208, 568, 189, 23);
		add(btnPescados);
		
		btnCarnes = new JButton("Conservas");
		btnCarnes.setBounds(404, 568, 189, 23);
		add(btnCarnes);
		
		btnPastas = new JButton("Otros");
		btnPastas.setBounds(603, 568, 192, 23);
		add(btnPastas);
		
		btnVolver = new JButton("Volver");
		btnVolver.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Ventana topFrame = (Ventana) SwingUtilities.getWindowAncestor(actual);
				JPanel next = new PanelPedidos(dao, controlador);
				topFrame.setContentPane(next);
			}
		});
		btnVolver.setBounds(357, 259, 89, 73);
		add(btnVolver);
		
		JButton btnCrearPedido = new JButton("Crear");
		btnCrearPedido.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				/*String selectedObject = (String) table_1.getModel().getValueAt(0, 0);
				System.out.println(selectedObject);*/
				String conjuntoDeProductos = "";
				
				System.out.println(table_1.getModel().getRowCount());
				//controlador.getControladorPedidos().crearPedido(conjuntoDeProductos);
				
			}
		});
		btnCrearPedido.setBackground(Color.GREEN);
		btnCrearPedido.setBounds(355, 440, 89, 73);
		add(btnCrearPedido);
	}
}
